package com.wuyan.locationdog.util.imageselecter

import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.widget.ImageView
import androidx.fragment.app.Fragment
import com.arcsoft.commonbase.ext.cropEngineLoad
import com.arcsoft.commonbase.ext.loadImage
import com.keqingkeji.app.util.imageselecter.ImageLoaderUtils
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.engine.CropFileEngine
import com.yalantis.ucrop.UCrop
import com.yalantis.ucrop.UCropImageEngine


class ImageCropEngine : CropFileEngine {

    override fun onStartCrop(
        fragment: Fragment,
        srcUri: Uri,
        destinationUri: Uri,
        dataSource: ArrayList<String>,
        requestCode: Int
    ) {
        val options: UCrop.Options = buildOptions()
        val uCrop = UCrop.of(srcUri, destinationUri, dataSource)
        uCrop.withOptions(options)
        uCrop.setImageEngine(object : UCropImageEngine {
            override fun loadImage(context: Context, url: String, imageView: ImageView) {
                if (!ImageLoaderUtils.assertValidRequest(context)) {
                    return
                }
                imageView.loadImage(url, 180, 180)
            }

            override fun loadImage(
                context: Context,
                url: Uri,
                maxWidth: Int,
                maxHeight: Int,
                call: UCropImageEngine.OnCallbackListener<Bitmap>
            ) {
                cropEngineLoad(context, url, maxWidth, maxHeight, {
                    call.onCall(it)
                }, {
                    call.onCall(null)
                })
            }
        })
        fragment.context?.let { uCrop.start(it, fragment, requestCode) }
    }

    private fun buildOptions(): UCrop.Options {
        val options = UCrop.Options()
        options.setHideBottomControls(true)
        options.setFreeStyleCropEnabled(false)
        options.setShowCropFrame(true)
        options.setShowCropGrid(true)
//        圆形头像裁剪模式
        options.setCircleDimmedLayer(false)

        options.withAspectRatio(1f, 1f)
//        options.setCropOutputPathDir(getSandboxPath())
        options.isCropDragSmoothToCenter(false)
        options.setSkipCropMimeType(PictureMimeType.ofGIF(), PictureMimeType.ofWEBP())
        options.isForbidCropGifWebp(true)
        options.isForbidSkipMultipleCrop(true)
        options.setMaxScaleMultiplier(100f)
//        if (selectorStyle != null && selectorStyle.getSelectMainStyle().getStatusBarColor() !== 0) {
//            val mainStyle: SelectMainStyle = selectorStyle.getSelectMainStyle()
//            val isDarkStatusBarBlack = mainStyle.isDarkStatusBarBlack
//            val statusBarColor = mainStyle.statusBarColor
//            options.isDarkStatusBarBlack(isDarkStatusBarBlack)
//            if (StyleUtils.checkStyleValidity(statusBarColor)) {
//                options.setStatusBarColor(statusBarColor)
//                options.setToolbarColor(statusBarColor)
//            } else {
//                options.setStatusBarColor(
//                    ContextCompat.getColor(
//                        getContext(),
//                        R.color.ps_color_grey
//                    )
//                )
//                options.setToolbarColor(ContextCompat.getColor(getContext(), R.color.ps_color_grey))
//            }
//            val titleBarStyle: TitleBarStyle = selectorStyle.getTitleBarStyle()
//            if (StyleUtils.checkStyleValidity(titleBarStyle.titleTextColor)) {
//                options.setToolbarWidgetColor(titleBarStyle.titleTextColor)
//            } else {
//                options.setToolbarWidgetColor(
//                    ContextCompat.getColor(
//                        getContext(),
//                        R.color.ps_color_white
//                    )
//                )
//            }
//        } else {
//            options.setStatusBarColor(ContextCompat.getColor(getContext(), R.color.ps_color_grey))
//            options.setToolbarColor(ContextCompat.getColor(getContext(), R.color.ps_color_grey))
//            options.setToolbarWidgetColor(
//                ContextCompat.getColor(
//                    getContext(),
//                    R.color.ps_color_white
//                )
//            )
//        }
        return options
    }
//    private fun getSandboxPath(): String {
//        val externalFilesDir: File? = GlobalContext.getContext().getExternalFilesDir("")
//        val customFile = File(externalFilesDir?.absolutePath ?: , "Sandbox")
//        if (!customFile.exists()) {
//            customFile.mkdirs()
//        }
//        return customFile.absolutePath + File.separator
//    }
}